<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/X-admin/css/font.css">
    <link rel="stylesheet" href="/X-admin/css/login.css">
    <link rel="stylesheet" href="/X-admin/css/xadmin.css">
    <script src="/X-admin/js/jquery.min.js"></script>
    <script src="/X-admin/lib/layui/layui.js"></script>
    <script src="/X-admin/js/xadmin.js"></script>
    <script src="../../js/myAjax.js"></script>
    <script src="../../js/wangEditor.min.js"></script>


</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item">
                <label for="productName" class="layui-form-label">
                    <span class="x-red">*</span>菜系选择</label>
                <div class="layui-input-inline layui-form" lay-filter="selFilter">
                    <select name="cuisine" id="cuisine">

                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="productName" class="layui-form-label">
                    <span class="x-red">*</span>产品名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="productName" name="productName" required="" lay-verify="required"
                           autocomplete="off"
                           class="layui-input"></div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="marketPrice" class="layui-form-label">
                    <span class="x-red">*</span>市场价</label>
                <div class="layui-input-inline">
                    <input type="text" id="marketPrice" name="marketPrice" required="" lay-verify="number"
                           autocomplete="off"
                           class="layui-input"></div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="normalPrice" class="layui-form-label">
                    <span class="x-red">*</span>市场价</label>
                <div class="layui-input-inline">
                    <input type="text" id="normalPrice" name="normalPrice" required="" lay-verify="number"
                           autocomplete="off"
                           class="layui-input"></div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>产品图片</label>
                <div class="layui-input-inline">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1">

                        </div>
                    </div>
                    <div style="width:300px;">
                        <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
                            <div class="layui-progress-bar" lay-percent=""></div>
                        </div>
                    </div>
                    <div><input type="file" onclick="loadImg()" id="imgLoad"></div>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>是否推荐</label>
                <div class="layui-input-inline">
                    <input type="checkbox" name="isRecommend" lay-skin="primary" title="是否推荐" value="1">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>是否置顶</label>
                <div class="layui-input-inline">
                    <!--                    <div class="layui-input-block">-->
                    <input type="radio" name="isTop" value="是" title="是">
                    <input type="radio" name="isTop" value="否" title="否" checked>
                    <!--                    </div>-->
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>产品描述</label>
                <div class="layui-input-inline" style="width: 300px">
                    <div id="content" style="width: 400px;"></div>

<!--                    <textarea id="content" style="display: none;"></textarea>-->
                </div>

            </div>
            <div class="layui-form-item">

                <button class="layui-btn" lay-filter="add" lay-submit="">增加</button>
            </div>
        </form>
    </div>
</div>
<script src="../../js/cuisine.js"></script>
<script src="../../js/createEditor.js"></script>
<script>

    function loadImg() {
        var formData = new FormData();
        formData.append("loginName",sessionStorage.getItem("username"));
        formData.append("productName", "fasd");
        formData.append("imgHref", $('#imgLoad')[0].files[0]);

        let data = myFileAjax("/upload", formData, "post");
        console.log(data.url);
        imgHref = data.url;
    }

    let imgHref = '';
    layui.use(['layedit', 'upload', 'form', 'layer', 'jquery', 'laydate'],
        function () {
            $ = layui.jquery;
            var form = layui.form
                , upload = layui.upload
                , layer = layui.layer;
            // var layedit = layui.layedit;
            // layedit.set({
            //     uploadImage: {
            //         url: '/upload' //接口url
            //         , type: '' //默认post
            //         , parseData: function (res) { //res 即为原始返回的数据
            //             console.log(res.url);
            //             return {
            //                 "code": 0, //解析接口状态
            //                 "msg": "", //解析提示文本
            //                 "count": "", //解析数据长度
            //                 "data": res.url //解析数据列表
            //             };
            //         }
            //     }
            // });
            // var ieditor = layedit.build('content');
            //自定义验证规则
            form.verify({});
//常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: '/upload' //改成您自己的上传接口
                , where: {
                    productName: "afds",
                    price: 111
                }
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                    // element.progress('demo', '0%'); //进度条复位
                    // layer.msg('上传中', {icon: 16, time: 0});
                }
                , done: function (res) {
                    //如果上传失败
                    // if (res.code > 0) {
                    //     return layer.msg('上传失败');
                    // }

                    //上传成功的一些操作
                    console.log(res.url)
                    imgHref = res.url;
                    //……
                    // $('#demoText').html('<img src="' + res.url + '">'); //置空上传失败的状态
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
            //监听提交
            form.on('submit(add)',
                function (data) {
                    console.log(data);
                    data = data.field;

                    var arr_box = [];
                    $('input[type=checkbox][name=isRecommend]:checked').each(function () {
                        arr_box.push($(this).val());
                    });
                    data.isRecommend = arr_box.toLocaleString();
                    data.imgHref = imgHref;
                    data.content=editor.txt.html();
                    //数组
                    console.log(data.content);

                    $.ajax({
                        url: '/back/product/add',
                        data: data,
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            //发异步，把数据提交给php
                            if (data.result == "success") {
                                layer.alert("增加成功", {
                                        icon: 6
                                    },
                                    function () {
                                        //关闭当前frame
                                        xadmin.close();

                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                    });
                            } else {
                                layer.alert("新增失败");
                            }

                        }
                    });

                    return false;
                });

        });</script>
<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>